install.packages("changepoint")

library(changepoint)


#setwd("~/Dropbox/Job Market Paper/IO Revisions/Replication_Files/Replication_Appendix/A_Fig_5")



data_q<-read.csv(file="data_fig_A5.csv")


num_states<-tapply(data_q$ID,data_q$Year,length)

years<-unique(data_q$Year)

c1<-cpt.mean(as.vector(num_states),test.stat="CUSUM",Q=70,penalty="SIC",method="SegNeigh")


pdf(file="Figure_A5.pdf")



plot(years,num_states,type="l",xlab="Year",ylab="Number of Stats")


abline(v=cpts(c1)*5+1100,lty=1)

c2means<-do.call(rbind,param.est(c1))

lines(c(1100,1100+cpts(c1)[1]*5) ,c(c2means[1],c2means[1]),lty=2)



lines(c(1100+cpts(c1)[1]*5,1790) ,c(c2means[2],c2means[2]),lty=2)


dev.off()